package com.system.manager.modules.assit.controller;


import com.system.manager.common.annotation.Anonymous;
import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.PageDTO;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.assit.entity.AssExpert;
import com.system.manager.modules.assit.service.IAssExpertService;
import com.system.manager.modules.assit.service.IProjectExpertService;
import com.system.manager.modules.assit.vo.ExpertRegister;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.authorized.vo.RoleMenuVo;
import com.system.manager.modules.projectLibrary.entity.Project;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxp
 * @since 2021-11-20
 */
@Api(tags = "专家API")
@RestController
@RequestMapping("/assit/expert")
public class AssExpertController {

    @Autowired
    private IAssExpertService expertService;

    @Autowired
    private IProjectExpertService projectExpertService;



    @ApiOperation("专家注册")
    @Anonymous
    @PostMapping("register")
    public Result register(@RequestBody ExpertRegister expertRegister){

        AssExpert expert = new AssExpert();
        BeanUtils.copyProperties(expertRegister, expert);

        try {

            expertService.register(expert);

        } catch (SysException sysException){

            sysException.printStackTrace();
            return Result.error(sysException);

        } catch(Exception e) {

            e.printStackTrace();
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation("获取所有的专家")
    @PreAuthorize({SubjectType.zx,SubjectType.EXPERT,SubjectType.PROJECT_LEADER,SubjectType.ENTERPRISE_ADMIN})
    @GetMapping("list/page")
    public PageDTO insert(@RequestParam(name = "page", required = false, defaultValue = "1")Integer page,
                          @RequestParam(name = "limit", required = false, defaultValue = "10")Integer size,
                          @RequestParam(name = "type", required = false)Integer type,
                          @RequestParam(name = "account",required = false)String account,
                          @RequestParam(name = "phone",required = false)String phone,
                          @RequestParam(name = "expertname", required =false)String expertname,
                          @RequestParam(name = "tchArea", required =false)String tchArea,
                          @RequestParam(name = "status", required =false)Integer status){

        PageDTO<AssExpert> pageDto =  expertService.selectLists(page, size, type,account, phone, expertname, tchArea, status);

        return pageDto;
    }


    @PreAuthorize({SubjectType.zx})
    @ApiOperation("增加专家")
    @PostMapping("insert")
    public Result insert(@RequestBody ExpertRegister expertRegister){

        try {
            AssExpert assExpert = new AssExpert();

            BeanUtils.copyProperties(expertRegister, assExpert);
            expertService.insert(assExpert);

            return Result.ok();
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }

    }

    @PreAuthorize({SubjectType.zx})
    @ApiOperation("删除专家")
    @GetMapping("delete/{expertId}")
    public Result delete(@PathVariable("expertId")Integer expertId){

        try {
            expertService.removeById(expertId);

//            AssExpert expert = expertService.getById(expertId);
//
//            userService.removeById(expert.getUserId());

            return Result.ok();
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }

    }

    @PreAuthorize({SubjectType.zx})
    @ApiOperation("查询专家信息")
    @GetMapping("select/{expertId}")
    public Result selectById(@PathVariable("expertId")Integer expertId){

        try {
            AssExpert assExpert = expertService.getById(expertId);

            return Result.ok(assExpert);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }

    }


    @PreAuthorize({SubjectType.zx})
    @ApiOperation("修改专家信息")
    @PostMapping("save")
    public Result save(@RequestBody AssExpert assExpert){

        try {
            expertService.updateById(assExpert);

            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }

    @PreAuthorize({SubjectType.zx})
    @ApiOperation("同意专家申请")
    @GetMapping("agree/{expertId}")
    public Result agree(@PathVariable("expertId")Integer expertId){

        expertService.agree(expertId);

        return Result.ok();
    }

    @PreAuthorize({SubjectType.zx})
    @ApiOperation("不同意专家申请")
    @GetMapping("disagree/{expertId}")
    public Result disagree(@PathVariable("expertId")Integer expertId){

        expertService.disagree(expertId);

        return Result.ok();
    }


    @PreAuthorize({SubjectType.zx,SubjectType.EXPERT,SubjectType.ENTERPRISE_ADMIN})
    @ApiOperation("项目授派专家")
    @PostMapping("post")
    public Result post(@RequestBody RoleMenuVo roleMenuVo){

        expertService.postProjectExpert(roleMenuVo.getMenusId(),roleMenuVo.getRoleId());

        return Result.ok();
    }

    //专家处理分配的项目
    @PreAuthorize({SubjectType.zx,SubjectType.EXPERT,SubjectType.ENTERPRISE_ADMIN})
    @ApiOperation("专家接受处理的项目")
    @GetMapping("accept")
    public PageDTO<Project> accept(@RequestParam(name = "page", required = false, defaultValue = "1")Integer page,
                                   @RequestParam(name = "limit", required = false, defaultValue = "10")Integer size,
                                   @CurrSubject Subject subject){

        PageDTO<Project> projectPageDTO = projectExpertService.selectByExpertId(page, size, subject);

        return projectPageDTO;
    }

    //修改状态
    @PreAuthorize({SubjectType.zx,SubjectType.EXPERT,SubjectType.ENTERPRISE_ADMIN})
    @ApiOperation("修改专家项目的状态值")
    @GetMapping("update/expertProject")
    public Result updateExpertProject(@CurrSubject Subject subject,Integer id){

        projectExpertService.updateState(subject,id);

        return Result.ok();
    }


}
